package com.cloud.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.cloud.train.business.domain.DailyTrainSeat;
import com.cloud.train.business.domain.DailyTrainSeatExample;
import com.cloud.train.business.domain.TrainSeat;
import com.cloud.train.business.domain.TrainStation;
import com.cloud.train.business.mapper.DailyTrainSeatMapper;
import com.cloud.train.business.req.DailyTrainCarriageSaveReq;
import com.cloud.train.business.req.DailyTrainSeatQueryReq;
import com.cloud.train.business.req.DailyTrainSeatSaveReq;
import com.cloud.train.business.resp.DailyTrainSeatQueryResp;
import com.cloud.train.common.resp.PageResp;
import com.cloud.train.common.util.SnowUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.logging.Log;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DailyTrainSeatService {
    @Resource
    private DailyTrainSeatMapper dailyTrainSeatMapper;

    @Resource
    private TrainSeatService trainSeatService;

    @Resource
    private TrainStationService trainStationService;
    public void save(DailyTrainSeatSaveReq req) {
        DateTime dateTime = new DateTime();
        DailyTrainSeat dailyTrainSeat = BeanUtil.copyProperties(req, DailyTrainSeat.class);
        if (ObjectUtil.isNull(req.getId())){
            dailyTrainSeat.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainSeat.setCreateTime(dateTime);
            dailyTrainSeat.setUpdateTime(dateTime);
            dailyTrainSeatMapper.insert(dailyTrainSeat);
        }
        else {
            dailyTrainSeat.setUpdateTime(dateTime);
            dailyTrainSeatMapper.updateByPrimaryKey(dailyTrainSeat);
        }
    }

    public PageResp<DailyTrainSeatQueryResp> queryList(DailyTrainSeatQueryReq req) {
        DailyTrainSeatExample dailyTrainSeatExample = new DailyTrainSeatExample();
        dailyTrainSeatExample.setOrderByClause("date desc, train_code asc, carriage_index asc, carriage_seat_index asc");
        DailyTrainSeatExample.Criteria criteria = dailyTrainSeatExample.createCriteria();
        if (ObjectUtil.isNotEmpty(req.getTrainCode())) {
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }

        PageHelper.startPage(req.getPage(), req.getSize());
        List<DailyTrainSeat> dailyTrainSeatList = dailyTrainSeatMapper.selectByExample(dailyTrainSeatExample);

        PageInfo<DailyTrainSeat> pageInfo = new PageInfo<>(dailyTrainSeatList);
        List<DailyTrainSeatQueryResp> list = BeanUtil.copyToList(dailyTrainSeatList, DailyTrainSeatQueryResp.class);

        PageResp<DailyTrainSeatQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    public void delete(long id) {
        dailyTrainSeatMapper.selectByPrimaryKey(id);
    }
    public void getDaily(Date date,String trainCode){
        log.info("生成车厢座位数据开始");
        DailyTrainSeatExample dailyTrainSeatExample = new DailyTrainSeatExample();
        dailyTrainSeatExample.createCriteria()
                .andDateEqualTo(date)
                .andTrainCodeEqualTo(trainCode);
        dailyTrainSeatMapper.deleteByExample(dailyTrainSeatExample);
        List<TrainStation> stationList = trainStationService.selectByTrainCode(trainCode);
        String shell = StrUtil.fillBefore("", '0', stationList.size() - 1);

        // 查出某车次的所有的座位信息
        List<TrainSeat> trainSeats = trainSeatService.selectByTrainCode(trainCode);
        if (CollUtil.isEmpty(trainSeats)) {
            log.info("该车次没有座位基础数据，生成该车次的座位信息结束");
            return;
        }
        for (TrainSeat trainSeat : trainSeats){
            DateTime dateTime = new DateTime();
            DailyTrainSeat dailyTrainSeat = BeanUtil.copyProperties(trainSeat, DailyTrainSeat.class);
            dailyTrainSeat.setUpdateTime(dateTime);
            dailyTrainSeat.setCreateTime(dateTime);
            dailyTrainSeat.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainSeat.setDate(date);
            dailyTrainSeat.setSell(shell);
            dailyTrainSeatMapper.insert(dailyTrainSeat);
            log.info("生成车厢座位数据结束{}",dailyTrainSeat);
        }
    }
    public long countSeat(Date date,String trainCode,String seatType){
        DailyTrainSeatExample dailyTrainSeatExample = new DailyTrainSeatExample();
//        dailyTrainSeatExample.setOrderByClause("carriage_seat_index asc");
        dailyTrainSeatExample.createCriteria()
                .andDateEqualTo(date)
                .andTrainCodeEqualTo(trainCode)
                .andSeatTypeEqualTo(seatType);
        long count = dailyTrainSeatMapper.countByExample(dailyTrainSeatExample);
        if (count == 0){
            return -1;
        }
        return count;
    }
//    public List<DailyTrainSeat> getSeat(Date date,String trainCode,Integer carriageIndex){
//        DailyTrainSeatExample dailyTrainSeatExample = new DailyTrainSeatExample();
//        dailyTrainSeatExample.setOrderByClause("carriage_seat_index asc");
//        dailyTrainSeatExample.createCriteria()
//                .andDateEqualTo(date)
//                .andTrainCodeEqualTo(trainCode)
//                .andCarriageIndexEqualTo(carriageIndex);
//        List<DailyTrainSeat> dailyTrainSeats = dailyTrainSeatMapper.selectByExample(dailyTrainSeatExample);
//        return dailyTrainSeats;
//    }
    public List<DailyTrainSeat> selectByCarriage(Date date, String trainCode, Integer carriageIndex) {
        DailyTrainSeatExample example = new DailyTrainSeatExample();
        example.setOrderByClause("carriage_seat_index asc");
        example.createCriteria()
                .andDateEqualTo(date)
                .andTrainCodeEqualTo(trainCode)
                .andCarriageIndexEqualTo(carriageIndex);
        return dailyTrainSeatMapper.selectByExample(example);
    }
}
